package priv.blackbibo.application.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

import androidx.annotation.Nullable;

import java.util.concurrent.locks.ReentrantLock;

import priv.blackbibo.application.R;

/**
 * Created by black on 2019-12-11.
 */
public class GosonService extends Service {

    private MediaPlayer player;

    private boolean isInit = false;
    private static final ReentrantLock LOCK = new ReentrantLock();

    private void init(){
        LOCK.lock();
        if (isInit){
            return;
        }
        player = MediaPlayer.create(this, R.raw.no_notice);
        isInit = true;
        LOCK.unlock();
    }

    private void start(){
        if (!isInit) {
            init();
        }
        player.setLooping(true);
        player.start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.start();
        return START_NOT_STICKY;
    }
}
